package net.zjitc.utils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by Administrator on 2017/11/30.
 */
public class MailUtils {

    public static void  sendMail(String to,String code) throws Exception {
        //发送邮件
        /**
         * 1. 建立一个会话session
         * 2. 创建一个一代表邮件对象的Message
         * 3. 发送
         */

        //1. 建立一个会话session
        Properties prop = new Properties();
        //163.邮箱服务器
        prop.setProperty("mail.host","smtp.qq.com");
        //授权
        prop.setProperty("mail.smtp.auth","true");
        prop.setProperty("mail.smtp.port", "587"); //端口号设置
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //16位口令
                return new PasswordAuthentication("1098011831@qq.com", "icvdlcnqspvufffe");
            }
        };
        Session session = Session.getInstance(prop,authenticator);

        //创建一个一代表邮件对象的Message
        Message message = new MimeMessage(session);
        //设置内容
        //发送给谁(谁发)
        message.setFrom(new InternetAddress("1098011831@qq.com"));

        //发谁
        message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
        message.addRecipient(Message.RecipientType.CC,new InternetAddress("188597447@qq.com"));
        //设置标题
        message.setSubject("来自软件1602的邮件");
        //设置内容(格式--html)
        String content = "<a href=\"http://localhost:8085/shop/activeCode?code="+code+"\">http://localhost:8085/shop/activeCode?code="+code+"</a>";
        message.setContent(content,"text/html;charset=utf-8");

        //发送
        Transport.send(message);


    }

}
